// for-await-of 异步迭代

// example: 同步迭代
const arr = ['es6','es5','es4']
arr[symbol.iterator] = function() {
  let nextIndex = 0
  return {
    next() {
      return nextIndex < arr.length ? {
        value: arr[nextIndex++],
        done: false
      }: {
        value: undefined,
        done: true
      }
    }
  }
}
for(let item of arr) {
  console.log(item);
}

function getPormise(time) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({
        value: time,
        done: false
      })
    },time)
  })
}
const arr = [getPromise(1000), getPormise(2000), getPormise(3000)]

arr[Symbol.asyncIterator] = function() {
  let nextIndex = 0
  return {
    next() {
      return {
        return nextIndex < arr.length ? arr[nextIndex++] :
        Promise.resolve({
          value: undefined,
          done: true
        })
      }
    }
  }
}

// 同步循环
for(let item of arr) {
  console.log(item);
}

// 异步循环  等一个异步结束后,再执行下一个循环
async function test() {
  for await(let item of arr) {
    console.log(item);
  }
}